﻿/*  Copyright 2012 PerceiveIT Limited
 *  This file is part of the Scryber library.
 *
 *  You can redistribute Scryber and/or modify 
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  Scryber is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Scryber source code in the COPYING.txt file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scryber.Drawing;
namespace Scryber.Components
{

    /// <summary>
    /// Defines a simple item value that can be 
    /// declared at the top of the document level
    /// </summary>
    public abstract class PDFDocumentItemValue : IKeyValueProvider
    {
        public Type ItemType { get; set; }

       
        [PDFAttribute("key")]
        public string Key { get; set; }

        protected PDFDocumentItemValue(Type nativeType)
        {
            this.ItemType = nativeType;
        }

        public abstract object GetNativeValue(string key, IPDFComponent comp);


    }

    [PDFParsableComponent("Int-Item")]
    public class PDFIntegerItemValue : PDFDocumentItemValue
    {
        [PDFAttribute("value")]
        [PDFElement()]
        public int Value { get; set; }

        public PDFIntegerItemValue()
            : base(typeof(int))
        {
            this.Value = 0;
        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            return this.Value;
        }
    }


    [PDFParsableComponent("String-Item")]
    public class PDFStringItemValue : PDFDocumentItemValue
    {
        [PDFAttribute("value")]
        [PDFElement()]
        public string Value { get; set; }

        public PDFStringItemValue()
            : base(typeof(string))
        {
            this.Value = string.Empty;
        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            return this.Value;
        }
    }


    [PDFParsableComponent("Double-Item")]
    public class PDFDoubleItemValue : PDFDocumentItemValue
    {
        [PDFAttribute("value")]
        [PDFElement()]
        public double Value { get; set; }

        public PDFDoubleItemValue()
            : base(typeof(double))
        {
            this.Value = 0.0;
        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            return this.Value;
        }
    }


    [PDFParsableComponent("Date-Item")]
    public class PDFDateItemValue : PDFDocumentItemValue
    {
        [PDFAttribute("value")]
        [PDFElement()]
        public DateTime Value { get; set; }

        public PDFDateItemValue()
            : base(typeof(int))
        {
            this.Value = DateTime.Now;
        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            return this.Value;
        }
    }


    [PDFParsableComponent("Unit-Item")]
    public class PDFUnitItemValue : PDFDocumentItemValue
    {
        [PDFAttribute("value")]
        [PDFElement()]
        public PDFUnit Value { get; set; }

        public PDFUnitItemValue()
            : base(typeof(PDFUnit))
        {
            this.Value = PDFUnit.Empty;
        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            return this.Value;
        }
    }



    [PDFParsableComponent("Color-Item")]
    public class PDFColorItemValue : PDFDocumentItemValue
    {
        [PDFAttribute("value")]
        [PDFElement()]
        public PDFColor Value { get; set; }

        public PDFColorItemValue()
            : base(typeof(PDFColor))
        {
            this.Value = null;
        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            return this.Value;
        }
    }


    [PDFParsableComponent("Thickness-Item")]
    public class PDFThicknessItemValue : PDFDocumentItemValue
    {
        [PDFAttribute("value")]
        [PDFElement()]
        public PDFThickness Value { get; set; }

        public PDFThicknessItemValue()
            : base(typeof(PDFColor))
        {
            this.Value = PDFThickness.Empty();
        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            return this.Value;
        }
    }

    [PDFParsableComponent("Enum-Item")]
    public class PDFEnumItemValue : PDFDocumentItemValue
    {
        [PDFAttribute("value")]
        [PDFElement()]
        public string EnumValue { get; set; }

        [PDFAttribute("type")]
        public string EnumType { get; set; }

        public PDFEnumItemValue()
            : base(typeof(Enum))
        {

        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            if (string.IsNullOrEmpty(this.EnumType))
                return null;
            else if (string.IsNullOrEmpty(this.EnumValue))
                return null;
            else
            {
                object parsed;
                try
                {
                    Type eType = Type.GetType(this.EnumType);
                    parsed = Enum.Parse(eType, this.EnumValue);
                }
                catch (Exception ex)
                {
                    throw new PDFParserException(string.Format(Errors.CouldNotParseValue_2, this.EnumValue, this.EnumType), ex);
                }

                return parsed;
            }

        }

    }

    [PDFParsableComponent("Xml-Item")]
    public class PDFXmlItemValue : PDFDocumentItemValue
    {
        [PDFElement()]
        public System.Xml.XmlNode XmlData { get; set; }

        public PDFXmlItemValue()
            : base(typeof(System.Xml.XmlNode))
        {
        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            return this.XmlData;
        }

    }


    [PDFParsableComponent("Template-Item")]
    public class PDFTemplateItemValue : PDFDocumentItemValue
    {
        [PDFElement()]
        [PDFTemplate()]
        public IPDFTemplate Template
        {
            get;
            set;
        }

        public PDFTemplateItemValue()
            : base(typeof(IPDFTemplate))
        {
        }

        public override object GetNativeValue(string key, IPDFComponent comp)
        {
            return this.Template;
        }

    }

}
